package com.gz.seckill.validator;

import com.gz.seckill.util.ValidatorUtil;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {
    private boolean required=false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required=constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (required){ //必填
            return ValidatorUtil.isMobile(value);
        }else { //非必填
            if (!StringUtils.hasText(value)){
                return true;
            }else{
                return ValidatorUtil.isMobile(value);
            }

        }
    }
}
